<template>
  <div>
      welcome
      <p>{{message}}</p>
      <el-button @click="logout">注销</el-button>
  </div>
</template>

<script>
export default {
    name: "Welcome" ,
    data(){
        return { message: '' }
    },
    // 组件内守护
    beforeRouteEnter( to , from , next ) {
        let login = sessionStorage.getItem( 'hasLoggedOn' );
        if( login ) {
            next();
        }
    },
    methods: {
        logout(){
            console.log( sessionStorage );
            sessionStorage.clear();
            console.log( sessionStorage );
            this.$router.push( '/login' );
        }
    },
    beforeMount(){
        this.axios.get( '/customer/welcome' ).then( resp => {
            // 后端返回的数据格式是 { login: true , message: '欢迎' }
            this.message = resp.data.message ;
        }).catch( error => {
            console.log( error );
        });
    }
}
</script>

<style>

</style>